Lectura de datos

setwd("C:/Users/mecat/Claro drive/2024-2/Modulo 3 Diplimado")
clima <- read.table(
"https://drvcruz.s3.us-east-2.amazonaws.com/SilwoodWeather.txt",
  header = T)
head(clima,2)
#write.csv(mpg, file = "climaSilverwoods.csv",row.names = FALSE )

Definiciones

#names(clima)
attach(clima)
head(clima, n=1L)
str(clima)
## 'data.frame':    6940 obs. of  5 variables:
##  $ upper: num  10.8 10.5 7.5 6.5 10 8 5.8 2.8 -0.8 1.5 ...
##  $ lower: num  6.5 4.5 -1 -3.3 5 3 -3.3 -5.5 -4.8 -1 ...
##  $ rain : num  12.2 1.3 0.1 1.1 3.5 0.1 0 0 0 0 ...
##  $ month: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ yr   : int  1987 1987 1987 1987 1987 1987 1987 1987 1987 1987 ...
month<-factor(month)

Gráficas tipo box Plot

plot(month,upper)

librerias

library(ggplot2)
month<-factor(month)
plot(month,upper)

Instalando Plotly

#install.packages("plotly")
library(plotly)
## 
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Gráfica Completa mes vs upper limit

p1 <- ggplot(clima, aes(x = factor(month), y=upper))+geom_boxplot()
ggplotly(p1)

Calculo de los cuantiles

quantile(clima$upper) # para el año
##   0%  25%  50%  75% 100% 
## -6.8 10.3 14.5 19.5 36.8

Repetimos para lower limit

pp <- ggplot(clima, aes(x = factor(month), y = lower)) + geom_boxplot()
ggplotly(pp)# quantile(clima$lower)

todos los años mes 3 en lower

sub1<-subset(clima, month=="3")## AHORA EL FACTOR ES AÑO
mes3 <- ggplot(sub1, aes(x = factor(yr), y = lower)) + geom_boxplot()
ggplotly(mes3)

todos los años mes 3 en lower

#median(sub1$lower)
boxplot(sub1$lower) #mes 3 para años 1987-2005
grid(10)

quantile(sub1$lower)#SOLO PARA EL MES 3
##   0%  25%  50%  75% 100% 
## -7.3  0.5  3.3  6.0 12.0
sd(sub1$lower)
## [1] 3.733525
(6-0.5)*0.75
## [1] 4.125

Graficando sus Frecuencias mes 3

hist(sub1$lower,freq = T, col = "lightblue",main = "lower levels month 3")
abline(v=mean(sub1$lower), col=3)
abline(v=6, col=2) #quantile(sub1$lower)

Ahora solo para el mes 3 año 1996

sub2 <- subset(sub1, yr==1996)
diasmes3 <- ggplot(sub2, aes(x=1:31,y=lower)) + geom_point() + labs(title="Diario de nivel Lower para el mes 3", y="Nivel Lower", x = "Dias del Mes")
ggplotly(diasmes3)

cuantiles

quantile(sub2$lower)
##    0%   25%   50%   75%  100% 
## -5.80 -1.90 -0.30  2.25  6.30
median(sub2$lower)
## [1] -0.3
summary(sub2$lower)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -5.8000 -1.9000 -0.3000  0.1903  2.2500  6.3000

Grafica cuantiles sobre histograma

hist(sub2$lower,freq = T, col = "lightblue",main = "lower levels month 3")
abline(v=median(sub2$lower), col="red")
abline(v= -1.9, col=4)
abline(v=2.25, col="green")

Grafica del Mes 3 de 1996

boxplot(sub2$lower)
grid()

Quantile rain

quantile(clima$rain)
##   0%  25%  50%  75% 100% 
##  0.0  0.0  0.0  1.6 59.5

Rain

p1 <- ggplot(clima, aes(x = factor(month), y = rain)) + geom_boxplot()
ggplotly(p1)

Analysis by year

p3 <- ggplot(clima, aes(x = factor(yr), y = upper)) + geom_boxplot()
ggplotly(p3)